<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { ElMessage } from 'element-plus'

const router = useRouter()
const userStore = useUserStore()

// 如果已经登录，直接跳转到主页
if (userStore.isLoggedIn()) {
  router.push('/home')
}

const loginForm = reactive({
  username: '',
  password: ''
})

const loading = ref(false)

const handleLogin = async () => {
  if (!loginForm.username || !loginForm.password) {
    ElMessage.warning('请输入用户名和密码')
    return
  }

  loading.value = true
  try {
    const success = userStore.login(loginForm.username, loginForm.password)
    if (success) {
      ElMessage.success('登录成功')
      router.push('/home/welcome')
    } else {
      ElMessage.error('用户名或密码错误')
    }
  } finally {
    loading.value = false
  }
}
</script>

<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <div class="logo">
          <svg class="icon" viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg">
            <path
              d="M512 85.333333c235.648 0 426.666667 191.018667 426.666667 426.666667s-191.018667 426.666667-426.666667 426.666667S85.333333 747.648 85.333333 512 276.352 85.333333 512 85.333333z m149.162667 222.208l-1.536 2.56-147.626667 268.373334-149.162667-268.373334a21.333333 21.333333 0 0 0-35.84-1.28l-1.28 2.133334-85.333333 170.666666a21.333333 21.333333 0 0 0 8.96 28.8l2.346667 1.066667a21.333333 21.333333 0 0 0 28.8-8.96l1.066666-2.346667 67.84-135.68 149.162667 268.373334a21.333333 21.333333 0 0 0 35.84 1.28l1.28-2.133334 147.626667-268.373333 67.84 135.68a21.333333 21.333333 0 0 0 38.4 1.28l1.28-2.346667a21.333333 21.333333 0 0 0-8.96-28.8l-2.346667-1.066667-85.333333-170.666666a21.333333 21.333333 0 0 0-32.128-1.536z"
              fill="currentColor"
            />
          </svg>
        </div>
        <h1>地质灾害信息系统</h1>
      </div>
      <div class="login-form">
        <el-form>
          <el-form-item>
            <el-input
              v-model="loginForm.username"
              placeholder="用户名"
              prefix-icon="el-icon-user"
            >
              <template #prefix>
                <svg class="icon" viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg">
                  <path
                    d="M858.5 763.6c-18.9-44.8-46.1-85-80.6-119.5-34.5-34.5-74.7-61.6-119.5-80.6-0.4-0.2-0.8-0.3-1.2-0.5C719.5 518 760 444.7 760 362c0-137-111-248-248-248S264 225 264 362c0 82.7 40.5 156 102.8 201.1-0.4 0.2-0.8 0.3-1.2 0.5-44.8 18.9-85 46-119.5 80.6-34.5 34.5-61.6 74.7-80.6 119.5C146.9 807.5 137 854 136 901.8c-0.1 4.5 3.5 8.2 8 8.2h60c4.4 0 7.9-3.5 8-7.8 2-77.2 33-149.5 87.8-204.3 56.7-56.7 132-87.9 212.2-87.9s155.5 31.2 212.2 87.9C779 752.7 810 825 812 902.2c0.1 4.4 3.6 7.8 8 7.8h60c4.5 0 8.1-3.7 8-8.2-1-47.8-10.9-94.3-29.5-138.2z"
                    fill="currentColor"
                  />
                  <path
                    d="M512 534c-97.2 0-176-78.8-176-176s78.8-176 176-176 176 78.8 176 176-78.8 176-176 176z m0-288c-61.9 0-112 50.1-112 112s50.1 112 112 112 112-50.1 112-112-50.1-112-112-112z"
                    fill="currentColor"
                  />
                </svg>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item>
            <el-input
              v-model="loginForm.password"
              type="password"
              placeholder="密码"
              prefix-icon="el-icon-lock"
              @keyup.enter="handleLogin"
            >
              <template #prefix>
                <svg class="icon" viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg">
                  <path
                    d="M832 464h-68V240c0-70.7-57.3-128-128-128H388c-70.7 0-128 57.3-128 128v224h-68c-17.7 0-32 14.3-32 32v384c0 17.7 14.3 32 32 32h640c17.7 0 32-14.3 32-32V496c0-17.7-14.3-32-32-32zM332 240c0-30.9 25.1-56 56-56h248c30.9 0 56 25.1 56 56v224H332V240z m460 600H232V536h560v304z"
                    fill="currentColor"
                  />
                  <path
                    d="M484 701v53c0 4.4 3.6 8 8 8h40c4.4 0 8-3.6 8-8v-53c12.1-8.7 20-22.9 20-39 0-26.5-21.5-48-48-48s-48 21.5-48 48c0 16.1 7.9 30.3 20 39z"
                    fill="currentColor"
                  />
                </svg>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" :loading="loading" @click="handleLogin" class="login-button">
              登录
            </el-button>
          </el-form-item>
          <div class="login-tips">
            <p>管理员账号: admin / admin123</p>
            <p>普通用户账号: guest / guest123</p>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  width: 100%;
  background: linear-gradient(135deg, #3498db, #8e44ad);
  overflow: hidden;
  position: relative;

  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1440 320"><path fill="%23ffffff10" d="M0,192L48,176C96,160,192,128,288,133.3C384,139,480,181,576,186.7C672,192,768,160,864,154.7C960,149,1056,171,1152,165.3C1248,160,1344,128,1392,112L1440,96L1440,320L1392,320C1344,320,1248,320,1152,320C1056,320,960,320,864,320C768,320,672,320,576,320C480,320,384,320,288,320C192,320,96,320,48,320L0,320Z"></path></svg>');
    background-position: bottom;
    background-repeat: no-repeat;
    background-size: 100% 40%;
    opacity: 0.5;
    z-index: 0;
  }
}

.login-box {
  width: 400px;
  padding: 40px;
  background-color: rgba(255, 255, 255, 0.9);
  border-radius: 10px;
  box-shadow: 0 15px 25px rgba(0, 0, 0, 0.1);
  z-index: 1;
  animation: fadeIn 0.5s ease-in-out;

  @media (max-width: 480px) {
    width: 90%;
    padding: 30px 20px;
  }
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(-20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.login-header {
  text-align: center;
  margin-bottom: 30px;

  .logo {
    display: flex;
    justify-content: center;
    margin-bottom: 15px;

    .icon {
      width: 60px;
      height: 60px;
      color: #409eff;
    }
  }

  h1 {
    font-size: 24px;
    color: #303133;
    margin: 0;
    font-weight: 500;
  }
}

.login-form {
  .el-form-item {
    margin-bottom: 25px;
  }

  .icon {
    width: 16px;
    height: 16px;
    margin-right: 8px;
    color: #909399;
  }

  .login-button {
    width: 100%;
    padding: 12px 0;
    font-size: 16px;
  }

  .login-tips {
    margin-top: 15px;
    text-align: center;
    color: #606266;
    font-size: 13px;
    line-height: 1.5;
  }
}
</style>
